package me.pcqpcq.android.msg;

import me.pcqpcq.android.mgr.ActivityMgr;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class MessageCentre {

	private static final int CLOSE_PROGRESS_DIALOG = 10;
	private static final int SHOW_PROGRESS_DIALOG = 11;
	private static final int UPDATE_PROGRESS_DIALOG_MSG = 12;
	private static final int SHOW_TOAST = 21;

	private static Activity act = ActivityMgr.getCurActivity();
	private static ProgressDialog progressDlg = new ProgressDialog(act);

	private static Handler msgHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case CLOSE_PROGRESS_DIALOG:
				if (progressDlg.isShowing())
					progressDlg.dismiss();
				break;

			case SHOW_PROGRESS_DIALOG:
				Bundle showPdBundle = msg.getData();
				progressDlg.setTitle(showPdBundle.get("title").toString());
				progressDlg.setMessage(showPdBundle.get("message").toString());
				progressDlg.setIndeterminate(showPdBundle.getBoolean("indeterminate"));
				progressDlg.setCancelable(showPdBundle.getBoolean("cancelable"));
				if (!progressDlg.isShowing() && !act.isFinishing())
					progressDlg.show();
				break;
				
			case UPDATE_PROGRESS_DIALOG_MSG:
				Bundle updatePdBundle = msg.getData();
				if (progressDlg.isShowing()) {
					progressDlg.setTitle(updatePdBundle.get("title").toString());
					progressDlg.setMessage(updatePdBundle.get("message").toString());
				}
				break;

			case SHOW_TOAST:
				Bundle showToastBundle = msg.getData();
				Toast.makeText(ActivityMgr.getCurActivity(), showToastBundle.getString("message"),
						showToastBundle.getInt("duration")).show();
				break;

			default:
				break;
			}
		}

	};

	/**
	 * 显示长时间的提示
	 * @param message 提示内容
	 * @param duration 显示的时长
	 */
	public static void showLongToast(String message) {
		Bundle bundle = new Bundle();
		bundle.putString("message", message);
		bundle.putInt("duration", Toast.LENGTH_LONG);
		Message msg = new Message();
		msg.setData(bundle);
		msg.what = SHOW_TOAST;
		msgHandler.sendMessage(msg);
	}
	
	/**
	 * 显示短时间的提示
	 * @param message 提示内容
	 * @param duration 显示的时长
	 */
	public static void showShortToast(String message) {
		Bundle bundle = new Bundle();
		bundle.putString("message", message);
		bundle.putInt("duration", Toast.LENGTH_SHORT);
		Message msg = new Message();
		msg.setData(bundle);
		msg.what = SHOW_TOAST;
		msgHandler.sendMessage(msg);
	}

	/**
	 * 显示“不确定，可取消”的进度框
	 * @param title 标题
	 * @param message 消息内容
	 */
	public static void updateProgressDlg(String title, String message) {
		Bundle bundle = new Bundle();
		bundle.putString("title", title);
		bundle.putString("message", message);
		Message msg = new Message();
		msg.setData(bundle);
		msg.what = UPDATE_PROGRESS_DIALOG_MSG;
		msgHandler.sendMessage(msg);
	}
	
	/**
	 * 显示“不确定，可取消”的进度框
	 * @param title 标题
	 * @param message 消息内容
	 */
	public static void showProgressDlg(String title, String message) {
		Bundle bundle = new Bundle();
		bundle.putString("title", title);
		bundle.putString("message", message);
		bundle.putBoolean("indeterminate", true);
		bundle.putBoolean("cancelable", true);

		Message msg = new Message();
		msg.setData(bundle);
		msg.what = SHOW_PROGRESS_DIALOG;
		msgHandler.sendMessage(msg);
	}

	/**
	 * 显示进度框
	 * @param title 标题
	 * @param message 消息内容
	 * @param indeterminate 确定性
	 * @param cancelable 是否可取消
	 */
	public static void showProgressDlg(String title, String message, boolean indeterminate, boolean cancelable) {
		Bundle bundle = new Bundle();
		bundle.putString("title", title);
		bundle.putString("message", message);
		bundle.putBoolean("indeterminate", indeterminate);
		bundle.putBoolean("cancelable", cancelable);

		Message msg = new Message();
		msg.setData(bundle);
		msg.what = SHOW_PROGRESS_DIALOG;
		msgHandler.sendMessage(msg);
	}

	/**
	 * 关闭进度框
	 */
	public static void closeProgressDlg() {
		msgHandler.sendEmptyMessage(CLOSE_PROGRESS_DIALOG);
	}

}
